package com.Telien.app.request.service;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.Teilen.app.db.main.DBWrapper;
import com.Telien.app.kml.KMLLoader;
import com.Telien.app.kml.KMLParser;
import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.object.Request;
import com.Tielen.app.matcher.service.MatcherService;

import de.micromata.opengis.kml.v_2_2_0.Coordinate;

public class RequestService {
	ArrayList<Request> systemRequests = new ArrayList();
	ArrayList<DriverRequest> mockDriverRequest = new ArrayList();
	KMLLoader kmlLoader;
	KMLParser kmlParser;
	
	public boolean addRequest(Request request){
		boolean status= false;
		if(request instanceof DriverRequest){
			try {
				request = processDriverRequest((DriverRequest)request);
				System.out.println();
				
				MatcherService matcherService = new MatcherService();
				if (matcherService.findMatch((DriverRequest)request)){					
					int selectedOption = JOptionPane.showConfirmDialog(null, "A Driver Request exists that is similar, would you like to request a ride?", "A match has been found!", JOptionPane.YES_NO_OPTION);
					if(selectedOption == JOptionPane.YES_OPTION){
						try{
							matcherService.createRideRequest((DriverRequest)request);
							return true;
						}
						catch(Exception e){
							e.printStackTrace();
						}
					}
				}
				
				persistDriverRequest(request);
			} catch (Exception e) {
				
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	private void persistDriverRequest(Request request) {
		DBWrapper wrapper = new DBWrapper();
		wrapper.createRide((DriverRequest)request);
		systemRequests.add(request);
	}

	private Request processDriverRequest(DriverRequest request) throws Exception {
		kmlLoader = new KMLLoader(request.getKmlPath());
		String kmlContent = kmlLoader.loadFileFromDisk();
		kmlParser  = new KMLParser(kmlContent);
		kmlParser.loadPoints();
		Coordinate startLocation = kmlParser.getStartLocation();
		Coordinate endLocation = kmlParser.getEndLocation();
		if(startLocation == null || endLocation == null){
			throw new Exception("Error Parsing KML no Endpoint");
		}
		request.setStartCord(startLocation);
		request.setEndCord(endLocation);
		return request;
	}
	
	public ArrayList<DriverRequest> getAllDriverRequests(){ // have it return something here
		DBWrapper db = new DBWrapper();
		return db.getAllDriverRequests();
	}
	
	public Request getDriverRequestById(String id){
		
		return null;
	}
	
	public boolean deleteRequestById(String id){
		
		return false;
	}
	
	public  ArrayList<DriverRequest> searchDriverRequest(String searchString){
		ArrayList<DriverRequest> searchArray = getAllDriverRequests();
		ArrayList<DriverRequest> keywordFound = new ArrayList();
		for(DriverRequest request: searchArray){
			if(request.toString().contains(searchString)){
				keywordFound.add(request);
			}
		}
		return keywordFound;
	}
	public boolean updateRequest(Request request){
		if(request instanceof DriverRequest){
			DriverRequest oldRequest = (DriverRequest) getDriverRequestById(request.getRequestUUID());
		}
		
		return true;
	}
	
	public void mockDriverRequestAdd(DriverRequest request){
		mockDriverRequest.add(request);
		System.out.println("Mock Add Request Added");
	}
	
	public ArrayList<DriverRequest> getMockDriverRequests(){
		return mockDriverRequest;
	}
	
	public  ArrayList<DriverRequest> mockSearchDriverRequest(String searchString){
		ArrayList<DriverRequest> searchArray = mockDriverRequest;
		ArrayList<DriverRequest> keywordFound = new ArrayList();
		for(DriverRequest request: searchArray){
			System.out.println(request.toString());
			if(request.toString().contains(searchString)){
				keywordFound.add(request);
			}
		}
		return keywordFound;
	}
	
	public String getLocation(String kmlPath){
		kmlLoader = new KMLLoader(kmlPath);
		String kmlContent = kmlLoader.loadFileFromDisk();
		kmlParser  = new KMLParser(kmlContent);
		kmlParser.loadPoints();
		Coordinate startLocation = kmlParser.getStartLocation();
		Coordinate endLocation = kmlParser.getEndLocation();
		if(startLocation != null && endLocation!=null)
			return "Start Location:"+startLocation.toString()+"\n"+"End Location:"+endLocation.toString();
		return "Invalid KML File";
	}

}
